from openpyxl import load_workbook, Workbook
import re
import time
import sys


class ChangeLog:
    def __init__(self):
        self.wb = load_workbook(sys.argv[1])
        self.sheet = self.wb.active
        self.row = self.sheet.max_row
        self.coloum = self.sheet.max_column
        self.all_num = 0
        self.not_handle_nums = 0
        self.all_datas = []
        self.handle_nums = []
        self.handle_datas = []

    def get_changelog_result(self, data):
        response = data.split('\n')
        new_data = []
        for i in response:
            if i.find('task') > -1 or i.find('任务号') > -1 or i.find('需求号') > -1 or i.find('commit:') > -1:
                continue
            tmp_data = re.findall("\d{5}", i)
            yongli = re.findall("【用例(\d{5})", i)
            tmp_data2 = re.findall("\d{6}", i) + yongli
            if yongli != []:
                tmp_data.remove(yongli[0])
            if tmp_data2 != []:
                tmp_data = self.clear(tmp_data, tmp_data2)
            if tmp_data != []:
                for i in tmp_data:
                    new_data.append(i)
        return new_data

    def write_excel(self):
        for i in self.handle_nums:
            datas = []
            for j in range(self.coloum):
                test_result = self.sheet.cell(i, j + 2).value
                if test_result == None:
                    test_result = ''
                    datas.append(test_result)
                    continue
                datas.append(test_result)

            datas.insert(0, i)
            self.all_datas.append(datas)
        print(self.all_datas)
        wb = Workbook()
        ws = wb.active
        title = ['所在行号', 'changelog', '所属组', '负责人', 'bug pass', 'bug failed', 'bug阻塞', '需求、任务pass', '需求任务failed',
                 '需求任务阻塞', '测试结果', '新增bug id', '备注', '镜像时间', '测试时间', '验证bug数量', '验证需求任务数']
        self.all_datas.insert(0, title)
        for index, value in enumerate(self.all_datas):
            for index2, value2 in enumerate(value):
                ws.cell(index + 1, index2 + 1, value=value2)
        wb.save("疑似未处理的数据.xlsx")
        wb.close()

    def handleData(self, all_bugs, handle_bug, test_result, line):
        """
        判断这行数据bug 号是否已经完全处理完，没处理的数据保存在一个列表中
        :param all_bugs: 全部要处理的bug id
        :param handle_bug:  已经处理的bug id
        :return: 返回差值和其信息
        """
        if len(all_bugs) == len(handle_bug) or test_result == "已沟通，可以不测":  # 如果两个列表长度相等
            self.all_num += len(all_bugs)
            return ""

        no_handled = list(set(all_bugs).difference(set(handle_bug)))
        if no_handled == []:
            return ""

        self.handle_datas.append(no_handled)
        self.handle_nums.append(line)

    def clear(self, list1, list2):
        list2 = [i[:5] for i in list2]
        new_list = list(set(list1).difference(set(list2)))
        return new_list

    def clear_bug_id(self, bugs):
        datas = []

        bugs = str(bugs)
        if bugs == [] or bugs == None:
            return []
        if len(bugs) == 5:
            return [bugs]
        bugss = bugs.split('\n')

        bugss = [i for i in bugss if len(i) > 0]

        for bug in bugss:
            if bug.find('，') > -1:
                tmp_bugs = bug.split('，')
                for i in tmp_bugs:
                    datas.append(i)
            if bug.find('、') > -1:
                tmp_bugs = bug.split('、')
                for i in tmp_bugs:
                    datas.append(i)
            if bug.find(',') > -1:
                tmp_bugs = bug.split(',')
                for i in tmp_bugs:
                    datas.append(i)
            else:
                datas.append(bug)
        if datas == []:
            datas = bugss
        return list(set(datas))

    def get_value(self):
        for line in range(self.row - 2):
            line = line + 3
            try:
                name = self.sheet.cell(line, 4).value
                if name.find(" ？")>-1 or name.find("?")>-1 or name.find("？")>-1:
                    continue
            except:
                pass
            test_result = self.sheet.cell(line, 11).value
            change_log_value = self.sheet.cell(line, 2).value  # 获取changelog内容
            change_log_result = self.get_changelog_result(change_log_value)  # 获取所有bug id

            bug_pass_id = self.sheet.cell(line, 5).value
            bug_failed_id = self.sheet.cell(line, 6).value
            bug_block_id = self.sheet.cell(line, 7).value
            if bug_pass_id == None or bug_pass_id == "":
                bug_pass_id = []
            else:
                bug_pass_id = self.clear_bug_id(bug_pass_id)  # 获取pass id
            if bug_failed_id == None or bug_failed_id == "":
                bug_failed_id = []
            else:
                bug_failed_id = self.clear_bug_id(bug_failed_id)  # 获取bug_failed_id
            if bug_block_id == None or bug_block_id == "":
                bug_block_id = []
            else:
                bug_block_id = self.clear_bug_id(bug_block_id)  # 获取bug_block_id

            have_handle_bug_id = bug_pass_id + bug_block_id + bug_failed_id  # 将已处理过的id 相加

            self.handleData(change_log_result, have_handle_bug_id, test_result, line)

    def main(self):
        self.get_value()
        self.write_excel()


if __name__ == '__main__':
    ss = ChangeLog()
    ss.main()

